const state = {
   routesArr: [] // 缓存的路由
}

const getters = { // 缓存路由获取
   getRoutes: state => state.routesArr
}

// 同步数据
const mutations = {
   setRoutesArr(state, data) {
      state.routesArr = data
   }
}

// 异步数据
const actions = {

   // 增加路由
   addRoute({
      commit,
      state
   }, data) {
      const arr = state.routesArr;

      let flag = false;

      arr.forEach(item => {
         if (item === data) {
            flag = true;
         }
      })
      if (!flag) {
         arr.push(data);
      }
      commit('setRoutesArr', arr);
   },

   // 移除指定路由
   removeRoute({
      commit,
      state
   }, data) {
      const arr = state.routesArr;

      let index = null;
      arr.forEach((item, num) => {
         if (item === data) {
            index = num;
         }
      })
      if (index) {
         arr.splice(index, 1);
      }

      commit('setRoutesArr', arr);
   },

   // 移除所有路由
   removeAllRoute({
      commit,
      state
   }, data) {
      commit('setRoutesArr', []);
   }
}

export default {
   namespaced: true, //用于在全局引用此文中的方法时标识这一个的文件名
   state,
   mutations,
   getters,
   actions
}